package time;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BaseTime {
    // JDK方法
    @Test
    public void calendar() {
        Calendar time01 = Calendar.getInstance();
        time01.add(Calendar.DATE, -1);
        System.out.println(time01.getTime());

        SimpleDateFormat format = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        String time02 = format.format(new Date(time01.getTimeInMillis()));
        System.out.println(time02);

        String time03 = format.format(new Date(System.currentTimeMillis()));
        System.out.println(time03);
    }

    // 当前时间，及转换
    @Test
    public void convert() {
        DateTime date01 = DateUtil.date();
        System.out.println(date01);
        Date date02 = DateUtil.date(Calendar.getInstance());
        System.out.println(date02);
        Date date03 = DateUtil.date(System.currentTimeMillis());
        System.out.println(date03);

        String now = DateUtil.now();
        System.out.println(now);
        String today = DateUtil.today();
        System.out.println(today);
    }

    // 时间临界点（开始、结束）
    @Test
    public void startEnd() {
        DateTime dateTime = DateUtil.beginOfDay(DateUtil.date());
        System.out.println(dateTime);
        DateTime endTime = DateUtil.endOfDay(DateUtil.date());
        System.out.println(endTime);


        DateTime dateTime1 = DateUtil.beginOfYear(DateUtil.date());
        System.out.println(dateTime1);
        DateTime dateTime2 = DateUtil.endOfYear(DateUtil.date());
        System.out.println(dateTime2);
    }

    // 区间计算
    @Test
    public void convertSection() {
        DateTime parse = DateUtil.parse("2022-01-01");
        long between = DateUtil.between(DateUtil.date(), parse, DateUnit.DAY, false);
        System.out.println(between);
    }

}
